package com.mzp.XbShop.controller;

import com.mzp.XbShop.common.entity.Result;
import com.mzp.XbShop.common.entity.StatusCode;
import com.mzp.XbShop.pojo.Delivery;
import com.mzp.XbShop.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 * @Description user_delivery 控制器层
 * @date 2021-04-19 10:42:24
 */
@RestController
@CrossOrigin
@RequestMapping("/delivery")
public class DeliveryController {

    @Autowired
    private DeliveryService deliveryService;

    /**
     * 根据用户id查找 该收货地址
     * @param userId
     * @return
     */
    @RequestMapping("/userDelivery/{userId}")
    public Result DeliveryfindByUserId(@PathVariable String userId){
        return new Result(true,StatusCode.OK,"查询成功", deliveryService.DeliveryfindByUserId(userId));
    }

    /**
     * 先将当前id的收货地址全部设置为0 在单独设置该收货地址的ismain 默认值
     */

    @RequestMapping("/setDefault/{id}/{userid}")
    public Result setDefault(@PathVariable String id,@PathVariable String userid){
       boolean flag=deliveryService.setZero(userid);
       if(!flag){
           return new Result(false,StatusCode.ERROR,"清0失败");
       }
        deliveryService.setOne(id);
        return new Result(true,StatusCode.OK,"清0成功");
    }

    /**
     * 拿到 自增长id  和当前用户id如果 已勾选 这设置为默认值  其他请0
     * @param delivery
     * @param @PathVariable String id
     * @return
     */
    @PostMapping("/save")
    public Result insert(@RequestBody Delivery delivery) {
        Integer num=deliveryService.insert(delivery);
        System.out.println("增加数据后的id为:"+delivery.getId());
        System.out.println("影响的列数:"+num);
        Integer deliveryId = delivery.getId();
        return new Result(true,StatusCode.OK,"添加成功",deliveryId);
    }

    /**
     * 没有选择 调用的接口。
     * @param delivery
     * @return
     */
    @PostMapping("/saveNoSelect")
    public Result insert1(@RequestBody Delivery delivery) {
        Integer num=deliveryService.insert(delivery);
        System.out.println("增加数据后的id为:"+delivery.getId());
        System.out.println("影响的列数:"+num);
        Integer deliveryId = delivery.getId();
        return new Result(true,StatusCode.OK,"添加成功",deliveryId);
    }

    /**
     * 逻辑删除
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable  Integer id) {
         deliveryService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

}
